package com.nc.delivery_project.client.composite;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by miroshnik on 12/29/2014.
 * Abstract PagingDataGrid class to set initial GWT DataGrid and Simple Pager with ListDataProvider *
 */
public abstract class PagingDataGrid<T> extends DataGrid {

    private DataGrid<T> dataGrid;
    
    private SimplePager pager;
    
    private String height;
    
    private ListDataProvider<T> dataProvider;
    
    private List<T> dataList;
    
    private DockPanel dock = new DockPanel();

    public PagingDataGrid() {
//        initWidget(dock);
        
        dataGrid = new DataGrid<T>();
        dataGrid.setWidth("100%");
        dataGrid.setEmptyTableWidget(new HTML("No Data to Display"));
        
        SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
        pager = new SimplePager(SimplePager.TextLocation.CENTER, pagerResources, false, 0, true);
        pager.setDisplay(dataGrid);
        
        dataProvider = new ListDataProvider<T>();
        dataProvider.setList(new ArrayList<T>());
        
        ColumnSortEvent.ListHandler<T> sortHandler = new ColumnSortEvent.ListHandler<T>(dataProvider.getList());

        initTableColumns(dataGrid, sortHandler);

        dataGrid.addColumnSortHandler(sortHandler);

        dataProvider.addDataDisplay(dataGrid);
        
        pager.setVisible(true);
        dataGrid.setVisible(true);

        dock.add(dataGrid, DockPanel.CENTER);
        dock.add(pager, DockPanel.SOUTH);
        dock.setWidth("100%");
        dock.setCellWidth(dataGrid, "100%");
        dock.setCellWidth(pager, "100%");
    }

    public void setEmptyTableWidget() {
        dataGrid.setEmptyTableWidget(new HTML(
                "The current request has taken longer than the allowed time limit. Please try your report query again."));
    }

    /**
     *
     * Abstract Method to implements for adding Column into Grid
     *
     * @param dataGrid
     * @param sortHandler
     */
    public abstract void initTableColumns(DataGrid<T> dataGrid, ColumnSortEvent.ListHandler<T> sortHandler);

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
        dataGrid.setHeight(height);
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
        List<T> list = dataProvider.getList();
        list.addAll(this.dataList);
        dataProvider.refresh();
    }

    public ListDataProvider<T> getDataProvider() {
        return dataProvider;
    }

    public void setDataProvider(ListDataProvider<T> dataProvider) {
        this.dataProvider = dataProvider;
    }

}